//
// Created by kriko on 2021/6/19.
//

#ifndef ASIO_NET_CHAT_MESSAGE_H
#define ASIO_NET_CHAT_MESSAGE_H

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>


class chat_message {
public:
    enum { header_length = 2 };
    enum { max_body_length = 512 };
public:
    chat_message() : _body_length(0) { }

    const char* data() const { return _data; }

    char* data() { return _data; }

     std::size_t length() const { return header_length + _body_length; }

     const char* body() const { return _data + header_length; }

     char* body() { return _data + header_length; }

     std::size_t body_length() const { return _body_length; }

     void body_length(uint16_t new_length) {
        _body_length = new_length;
        if (_body_length > max_body_length)
            _body_length = max_body_length;
    }

    bool decode_header() {
        char header[header_length + 1] = "";
        std::strncat(header, _data, header_length);
        _body_length = header[1] << 8;
        _body_length = _body_length | header[0];
        if (_body_length > max_body_length) {
            _body_length = 0;
            return false;
        }
        return true;
    }

    void encode_header() {
        char header[header_length + 1] = "";
        std::sprintf(header, "%2d", static_cast<uint16_t>(_body_length));
        std::memcpy(_data, header, header_length);
    }

private:
    char _data[header_length + max_body_length];
    uint16_t _body_length;

};

#endif //ASIO_NET_CHAT_MESSAGE_H
